package com.yourannet.android;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.yourannet.android.commons.App;
import com.yourannet.android.service.ListenNetStateService;

public class MainActivity extends Activity implements OnClickListener {

	private TextView tip;
	private Button exitApp,startService,exitService;
	App myApp;
	private Handler mHandler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			switch(msg.arg1){
			case 0:
				tip.setText(msg.obj.toString());
				break;
			}
		}
		
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		tip = (TextView)findViewById(R.id.txt_tip);
		exitApp = (Button)findViewById(R.id.btn_exit);
		exitApp.setOnClickListener(this);
		exitService = (Button)findViewById(R.id.btn_exit_service);
		exitService.setOnClickListener(this);
		startService = (Button)findViewById(R.id.btn_start_service);
		startService.setOnClickListener(this);
		myApp = ((App)getApplicationContext());
		new Thread(new Runnable(){

			@Override
			public void run() {
				// TODO Auto-generated method stub
				while(true){
					Message msg = mHandler.obtainMessage();
					msg.arg1 = 0;
					if(myApp.getNetState()){
						msg.obj = "网络正常！";
					}else{
						msg.obj = "网络不可用！";
					}
					 try {
						Thread.sleep(5*1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					 msg.sendToTarget();
				}
			}
			
		}).start();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		this.stopService(new Intent(this,ListenNetStateService.class));
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onRestoreInstanceState(savedInstanceState);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		// TODO Auto-generated method stub
		super.onSaveInstanceState(outState);
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
		case R.id.btn_exit:
			this.stopService(new Intent(this,ListenNetStateService.class));
			finish();
			break;
		case R.id.btn_start_service:
			this.startService(new Intent(this,ListenNetStateService.class));
			break;
		case R.id.btn_exit_service:
			this.stopService(new Intent(this,ListenNetStateService.class));
			break;
		}
	}

}
